package com.example.houduan.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.houduan.entity.User;
import com.example.houduan.mapper.UserMapper;
import com.example.houduan.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anonymous
 * @since 2024-08-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public void registerUser(User reg) {
     baseMapper.addUser(reg);

    }

    @Override
    public void logoutUser(Long id) {
        baseMapper.logoutUser(id);
    }



    @Override
    public boolean checkByEmail(String email) {
        if (!baseMapper.checkByEmail(email).isEmpty())return true;

        return false;
    }

    @Override
    public void updatapassword(String email,String password) {
        baseMapper.updateByEmail(email, SecureUtil.md5(password));

    }

    @Override
    public User checkByUsername(String username) {

        return  baseMapper.checkByUsername(username);
    }

    @Override
    public boolean updatapasswordByPassword(String username, String password, String psd) {
       if (!baseMapper.checkByUsernameAndPassword(username,SecureUtil.md5(password)).isEmpty()){
           baseMapper.updataBPassword(username,SecureUtil.md5(password),SecureUtil.md5(psd));
           return true;
       }
       return  false;
    }
    @Override
    public User findByUsername(User user){
        return baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()));

    }

    @Override
    public void updateUser(User user) {
        baseMapper.updateById(user);
    }

    @Override
    public User findUserById(Long userId) {
        return baseMapper.selectById(userId);
    }



}
